****************************************************************************************************************
**																											  **
** This do file is part of the replication material for the following article: 								  **
**  "Is There an Ideological Asymmetry in the Incumbency Effect? Evidence from U.S. Congressional Elections." **
** 		Authors: Morisi, Davide, Jost, John, Panagopoulos, Costas, and Jussi Valtonen		    			  **
** 		Journal: Social Psychological and Personality Science																	  **
**																											  **
** This file replicates the following step of the analysis: 												  **
**																											  **
**		RECODING CCES 2014 ORIGINAL DATASET			   													  	  **
**																											  **
****************************************************************************************************************

*Set working directory
cd "C:\Users\Davide Morisi\Dropbox\datasets\CCES"

*Open original dataset
*Schaffner, Brian; Ansolabehere, Stephen, 2015, "CCES Common Content, 2014", https://doi.org/10.7910/DVN/XFXJVY, Harvard Dataverse, V4, UNF:6:WvvlTX+E+iNraxwbaWNVdg== [fileUNF] 
*Downloaded on January 16, 2020 from https://dataverse.harvard.edu/dataset.xhtml?persistentId=doi%3A10.7910/DVN/XFXJVY
use "2014\CCES14_Common_Content_Validated.dta", clear

***
*covariates

*gender
fre gender // 2=female
*age
sum birthyr
*education
fre educ 
*race
fre race
recode race 4/8=99, gen(racer) // 99=other
*religion
fre religpew
recode religpew 3/8=3 12=3 9=4 10=5 11=6, gen(religpewr)
fre religpewr
*economy
fre CC14_302
rename CC14_302 economy
*state
fre inputstate
rename inputstate state
clonevar state14 = state
*weight
rename weight weight1


*********************
*partisanship / ideology

*party of respondent - profile
*3 categories
*1 democrat, 2 republican, 3 independent, 4 other, 5 dk
fre pid3
*7 categories
*1 strong dem, 2 not strong dem, 3 lean dem, 4 indep, 5 lean rep, 6 not strong rep, 7 strong rep, 8 dk 
fre pid7
ta pid7 pid3 // leaning partisans coded as independent/other/dk

*party id post
*3 categories (note: cat 5 dk missing)
fre CC421a // pid3
rename CC421a pid3post
*5 categories
fre CC421b // leaners
rename CC421b leaners_prof_14
ta pid3post if leaners_prof_14!=. // asked only for independents/others
fre pid3post leaners_prof_14
gen pid5post = 1 if pid3post==1
replace pid5post = 2 if pid3post>2 & leaners_prof_14==1
replace pid5post = 3 if pid3post==3 & leaners_prof_14==3
replace pid5post = 4 if pid3post>2 & leaners_prof_14==2
replace pid5post = 5 if pid3post==2
*1 dem, 2 lean dem, 3 indep, 4 lean rep, 5 rep
fre pid5post

*ideology profile
fre ideo5

*ideology from PRE-ELECTION SURVEY
*1 very lib, 2 lib, 3 somewhat lib, 4 mod, 5 somewhat con, 6 con, 7 very con, 8dk
fre CC334A
rename CC334A ideo7


*******
*vote post
*turnout
fre CC401
rename CC401 turnout14
clonevar turnout = turnout14
*senate
fre CC410b
rename CC410b votesen14
clonevar vote_sen = votesen14

*house
fre CC412
rename CC412 votehou14
clonevar vote_hou = votehou14


***
*contextual var - SENATE

*incumbent candidates
des SenCand1Incumbent_post SenCand2Incumbent_post
fre SenCand1Incumbent_post SenCand2Incumbent_post
encode(SenCand1Incumbent_post), gen(SenCand1Incumbent_postr)
encode(SenCand2Incumbent_post), gen(SenCand2Incumbent_postr)
fre SenCand1Incumbent_postr SenCand2Incumbent_postr
recode SenCand1Incumbent_postr 1=0 2=1
recode SenCand2Incumbent_postr 1=0 2=1
label de incumb 0"not incumbent" 1"incumbent", modify
label values SenCand1Incumbent_postr incumb
label values SenCand2Incumbent_postr incumb
fre SenCand1Incumbent_post SenCand1Incumbent_postr
fre SenCand2Incumbent_post SenCand2Incumbent_postr
ta SenCand1Incumbent_postr SenCand2Incumbent_postr

*party of candidate
des SenCand1Party_post SenCand2Party_post
fre SenCand1Party_post SenCand2Party_post
encode SenCand1Party_post, gen(SenCand1Party_postr)
encode SenCand2Party_post, gen(SenCand2Party_postr)
fre SenCand1Party_postr SenCand2Party_postr
recode SenCand1Party_postr 1=1 2=3
recode SenCand2Party_postr 1=2
label de partycand 1"dem. candidate" 2"rep. candidate" 3"other candidate", modify
label val SenCand1Party_postr partycand
label val SenCand2Party_postr partycand
fre SenCand1Party_postr SenCand2Party_postr

*republican vs dem incumbent
fre SenCand1Party_postr SenCand1Incumbent_postr
fre SenCand2Party_postr SenCand2Incumbent_postr
gen inc_rep_sen = 0 if (SenCand1Party_postr==1 & SenCand2Party_postr==2) & (SenCand1Incumbent_postr==1 & SenCand2Incumbent_postr==0)
replace inc_rep_sen = 1 if (SenCand1Party_postr==1 & SenCand2Party_postr==2) & (SenCand1Incumbent_postr==0 & SenCand2Incumbent_postr==1)
fre inc_rep_sen // 0= dem inc (rep not inc), 1= rep inc (dem not inc)
*including districts with no dem/rep incumbents
ta SenCand1Incumbent_postr SenCand2Incumbent_postr if (SenCand1Party_postr==1 & SenCand2Party_postr==2)
gen inc_all_sen = inc_rep_sen
recode inc_all_sen 0=-1
replace inc_all_sen = 0 if (SenCand1Party_postr==1 & SenCand2Party_postr==2) & (SenCand1Incumbent_postr==0 & SenCand2Incumbent_postr==0)
ta inc_all_sen // -1=dem inc, 0=inc not dem nor rep, 1=rep inc

*keep only states with elections, not open elections
ta state inc_rep_sen, nol
*filter
/*gen count2=1 if inc_rep!=.
egen count_sum2 = sum(count2), by(state)
fre state if count_sum<10 & inc_rep!=.
ta state inc_rep if count_sum<=10, nol
ta state inc_rep if count_sum>10, nol
gen filter_sen2=1 if inc_rep!=.
replace filter_sen2=2 if count_sum<10 & inc_rep!=.
replace filter_sen2=3 if (state==21 | state==23 | state==40 | state==48 | state==56) & inc_rep==0
replace filter_sen2=3 if (state==5 | state==8 | state==17 | state==27 | state==34 | state==37 | state==51) & inc_rep==1
ta filter_sen2
ta state inc_rep if filter_sen2==1
clonevar filter_sen14 = filter_sen*/
ta SenCand1Name_post
bys state: ta SenCand1Name_post if inc_rep_sen!=.
gen count=1 if inc_rep_sen!=.
egen count_sum = sum(count), by(state SenCand1Name_post)
gen filter_sen=1 if inc_rep_sen!=.
replace filter_sen=2 if count_sum<10 & inc_rep_sen!=.
ta state inc_rep_sen
ta state inc_rep_sen if filter_sen==1
clonevar filter_sen14 = filter_sen


******
*contextual var HOUSE
fre state
list HouseCand1Name_post HouseCand1IncumbentNum_post HouseCand2Name_post HouseCand2IncumbentNum_post state in 1/20

*incumbent candidates
des HouseCand1IncumbentNum_post HouseCand2IncumbentNum_post
fre HouseCand1IncumbentNum_post HouseCand2IncumbentNum_post
gen HouseCand1Incumbent_postr = HouseCand1IncumbentNum_post
gen HouseCand2Incumbent_postr = HouseCand2IncumbentNum_post
fre HouseCand1Incumbent_postr HouseCand2Incumbent_postr
label val HouseCand1Incumbent_postr incumb
label val HouseCand2Incumbent_postr incumb
ta HouseCand1Incumbent_postr HouseCand2Incumbent_postr

*party of candidate
des HouseCand1Party_post HouseCand2Party_post
fre HouseCand1Party_post HouseCand2Party_post
encode HouseCand1Party_post, gen(HouseCand1Party_postr)
encode HouseCand2Party_post, gen(HouseCand2Party_postr)
fre HouseCand1Party_postr HouseCand2Party_postr
recode HouseCand1Party_postr 1=1 2/4=3 5=2
recode HouseCand2Party_postr 1=3 2=1 3/6=3 7=2 8=3
label val HouseCand1Party_postr partycand
label val HouseCand2Party_postr partycand
fre HouseCand1Party_postr HouseCand2Party_postr

*republican incumbent vs. dem incumbent
*note: with this var we restrict analysis to seats with dem vs. rep AND with inc. vs. not inc.
fre HouseCand1Party_postr HouseCand1Incumbent_postr
fre HouseCand2Party_postr HouseCand2Incumbent_postr
ta HouseCand1Party_postr HouseCand2Party_postr // no cases when candidate 1 is republican and candidate 2 is democrat
gen inc_rep_hou = 0 if (HouseCand1Party_postr==1 & HouseCand2Party_postr==2) ///
& (HouseCand1Incumbent_postr==1 & HouseCand2Incumbent_postr==0)
replace inc_rep_hou = 1 if (HouseCand1Party_postr==1 & HouseCand2Party_postr==2) ///
& (HouseCand1Incumbent_postr==0 & HouseCand2Incumbent_postr==1)
fre inc_rep_hou
ta HouseCand1Incumbent_postr HouseCand2Incumbent_postr if inc_rep_hou!=.
*including districts with no dem/rep incumbents
ta HouseCand1Incumbent_postr HouseCand2Incumbent_postr if (HouseCand1Party_postr==1 & HouseCand2Party_postr==2) 
gen inc_all_hou = inc_rep_hou
recode inc_all_hou 0=-1
replace inc_all_hou = 0 if (HouseCand1Party_postr==1 & HouseCand2Party_postr==2) & (HouseCand1Incumbent_postr==0 & HouseCand2Incumbent_postr==0)
fre inc_all_hou // -1=dem inc, 0=inc not dem nor rep, 1=rep inc

*keep only states with elections, not open elections
ta state inc_rep_hou
*filter
gen count1=1 if inc_rep_hou!=.
egen count_sum1 = sum(count1), by(state HouseCand1Name_post)
gen filter_hou=1 if inc_rep_hou!=.
replace filter_hou=2 if count_sum1<10 & inc_rep_hou!=.
ta state inc_rep_hou if filter_hou==1
*double check
bys state: ta HouseCand1Name_post if filter_hou==1 // e.g. montana missing because no incumbent
fre state if HouseCand1Name_post=="John Lewis" // seat without incumbent
fre state if HouseCand1Name_post=="John Lewis" & filter_hou==1 // removed when filter applied
ta state inc_rep_hou if filter_hou==1
clonevar filter_hou14 = filter_hou


*******************
*combined variables > vote + incumbent status

*vote + incumbent status, all options - SENATE
ta vote_sen inc_all_sen
fre turnout vote_sen
fre inc_all_sen // -1=dem inc, 0=inc not dem nor rep, 1=rep inc
fre SenCand1Party_postr SenCand2Party_postr
gen votinc_sen_all = 0 if turnout<5 | vote_sen==5 | vote_sen==6 // did not vote
replace votinc_sen_all = 10 if vote_sen==1 & inc_all_sen==-1 & SenCand1Party_postr==1
replace votinc_sen_all = 11 if vote_sen==1 & inc_all_sen==0 & SenCand1Party_postr==1
replace votinc_sen_all = 12 if vote_sen==1 & inc_all_sen==1 & SenCand1Party_postr==1
replace votinc_sen_all = 20 if vote_sen==2 & inc_all_sen==-1
replace votinc_sen_all = 21 if vote_sen==2 & inc_all_sen==0
replace votinc_sen_all = 22 if vote_sen==2 & inc_all_sen==1
*label var votinc_sen_all "Voted for dem/rep by incumbent status - SENATE"
*label de votinc_sen_all 0"did not vote" 10"vote dem, dem inc"  11"vote dem, no inc"  12"vote dem, rep inc" ///
*20"vote rep, dem inc"  21"vote rep, no inc"  22"vote rep, rep inc" 
*label val votinc_sen_all votinc_sen_all
fre votinc_sen_all

*vote + incumbent status, all options - HOUSE
ta vote_hou inc_all_hou
fre vote_hou
fre inc_all_hou // -1=dem inc, 0=inc not dem nor rep, 1=rep inc
fre HouseCand1Party_postr HouseCand2Party_postr
tab1 HouseCand1Party_postr if vote_hou==1 & inc_rep_hou!=.
tab1 HouseCand1Party_postr if vote_hou==1 & (inc_all_hou==-1 | inc_all_hou==1)
tab1 HouseCand2Party_postr if vote_hou==2 & inc_rep_hou!=.
tab1 HouseCand2Party_postr if vote_hou==2 & (inc_all_hou==-1 | inc_all_hou==1)
gen votinc_hou_all = 0 if turnout<5 | vote_hou==8 | vote_hou==9 // did not vote
replace votinc_hou_all = 10 if vote_hou==1 & inc_all_hou==-1 & HouseCand1Party_postr==1
replace votinc_hou_all = 11 if vote_hou==1 & inc_all_hou==0 & HouseCand1Party_postr==1 
replace votinc_hou_all = 12 if vote_hou==1 & inc_all_hou==1 & HouseCand1Party_postr==1
replace votinc_hou_all = 20 if vote_hou==2 & inc_all_hou==-1 & HouseCand2Party_postr==2
replace votinc_hou_all = 21 if vote_hou==2 & inc_all_hou==0 & HouseCand2Party_postr==2
replace votinc_hou_all = 22 if vote_hou==2 & inc_all_hou==1 & HouseCand2Party_postr==2
*label var votinc_hou_all "Voted for dem/rep by incumbent status - HOUSE"
*label val votinc_hou_all votinc_sen_all
fre votinc_hou_all


******
*variables for merging datasets
gen year = 2014

rename V101 caseid14
clonevar caseid = caseid14

ta tookpost


****
*create congressional districts
decode state, gen(stated)
drop if stated=="District of Columbia"
encode stated, gen(staten)
label var staten "States from 1 to 50 in alphabetical order"
fre staten // alphabetical order, from 0 to 50

*combine with district
rename cdid_post cd2014str
encode cd2014str, gen(cd2014)
fre cd2014str // problem value 1 instead of 01, 2 instead of 02, etc.
fre cd2014str if staten==2

*turn 1 into 01, 2 into 02 etc.
gen cd2014_first = (substr(cd2014str,1,1))
gen cd2014_second = (substr(cd2014str,2,1))
encode cd2014_first, gen(cd2014_firsten)
encode cd2014_second, gen(cd2014_seconden)
fre cd2014_first if cd2014_seconden==.
gen value0 = 0
egen cd2014_first_correct=concat(value0 cd2014_first) if cd2014_firsten!=. & cd2014_seconden==.
fre cd2014_first cd2014_first_correct if cd2014_seconden==.

gen cd2014str_correct = cd2014_first_correct if cd2014_seconden==.
replace cd2014str_correct = cd2014str if cd2014_seconden!=.
label var cd2014str_correct "Post  Congressional district 2014 1=01"
fre cd2014str cd2014str_correct if staten==2
fre cd2014str cd2014str_correct if staten==9
fre cd2014str cd2014str_correct if staten==50

egen stcd=concat(staten cd2014str_correct)

*double check > all correct
fre stcd if staten==9 // florida
fre stcd if staten==9 & cd2014!=.
fre stcd if staten==45 // vermont
fre stcd if staten==45 & cd2014!=.

*
rename stcd stcd_string
gen stcd = real(stcd_string) // turn into numeric
label var stcd_string "State (1 to 50 alphabetical) + congr. district (01 to n) STRING"
label var stcd "State (1 to 50 alphabetical) + congr. district (01 to n)"


***
/*merge with contextual var

save "CCES_2014_recoded2.dta", replace
import excel "C:\Users\Davide Morisi\Dropbox\research_inprogress\incumbency_effect\Other files\hr4618.xls", sheet("2014") firstrow clear
sum year stcd inc pwin dv dvp fr po1 po2 redist dexp rexp dpres switchb switcha
rename po1 po1_orig
rename po2 po2_orig
gen po1 = real(po1_orig)
gen po2 = real(po2_orig)
sum year stcd inc pwin dv dvp fr po1 po2 redist dexp rexp dpres switchb switcha
list po2 po2_orig in 50/70
list po1 po1_orig in 100/120
drop year po1_orig po2_orig switchb switcha
save "C:\Users\Davide Morisi\Dropbox\research_inprogress\incumbency_effect\Replication do files\Data\house_data_2014.dta", replace
use "CCES_2014_recoded2.dta", clear
*/

*merge info about previous election
merge m:1 stcd using "C:\Users\Davide Morisi\Dropbox\research_inprogress\incumbency_effect\Replication do files\Data\house_data_2012.dta" // all matched from using
drop pwin dv dvp fr redist dexp rexp dpres _merge
rename inc inc_previous
rename po1 po1_previous
rename po2 po2_previous

*post election
merge m:1 stcd using "C:\Users\Davide Morisi\Dropbox\research_inprogress\incumbency_effect\Replication do files\Data\house_data_2016.dta" // all matched
drop pwin dv dvp fr redist dexp rexp dpres _merge
rename inc inc_post
rename po1 po1_post
rename po2 po2_post

merge m:1 stcd using "C:\Users\Davide Morisi\Dropbox\research_inprogress\incumbency_effect\Replication do files\Data\house_data_2014.dta" // all matched from using
drop _merge

***
*voter registration status post
fre votereg_post
recode votereg_post (2 3=0 "No/DK") (1=1 "Yes"), gen(register_post)
label var register_post "Registered to vote? (post)"
fre register_post

***
*job approval
*Please indicate whether you approve or disapprove of the job that each of the following are doing.
* 1 Strongly approve
* 2  Approve
* 3 Disapprove
* 4 Strongly disapprove
* 5 never heard

*House representative
fre CC14_315a 
rename CC14_315a approval_house

*senate
*2 senators for each state. 
fre CC14_315b CC14_313c
rename CC14_315b approval_sen1
rename CC14_313c approval_sen2

***
*keep only selected variables
keep gender birthyr educ racer religpewr state state14 weight1 economy ///
pid3 pid7 pid3post pid5post leaners_prof_14 ///
ideo5 ideo7 ///
turnout turnout14 vote_sen votesen14 vote_hou votehou14 ///
SenCand1Incumbent_postr SenCand2Incumbent_postr SenCand1Party_postr SenCand2Party_postr ///
inc_rep_sen inc_all_sen filter_sen filter_sen14 ///
HouseCand1Incumbent_postr HouseCand2Incumbent_postr HouseCand1Party_postr HouseCand2Party_postr ///
inc_rep_hou inc_all_hou filter_hou filter_hou14 ///
votinc_sen_all votinc_hou_all ///
year caseid caseid14 tookpost ///
staten ///
cd2014 cd2014str cd2014str_correct stcd_string stcd ///
inc pwin dv dvp fr po1 po2 redist dexp rexp dpres ///
register_post approval_house approval_sen1 approval_sen2 ///
inc_previous po1_previous po2_previous ///
inc_post po1_post po2_post


*save recoded file
save "CCES_2014_recoded2.dta", replace

